package com.maben.app;

import com.alibaba.fastjson.JSON;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

/**
 * axis调用学习
 * 注意：
 *  1. 使用new QName包起来参数，而且必须加上namespaceURI参数，否则也会报错
 */
public class Test002_axis {
    public static void main(String[] args) throws Exception {
        test001();
        System.out.println("--------------------------");
        test002();
    }

    public static void test001() throws Exception {
        Service service = new Service();
        Call call = (Call) service.createCall();
        call.setTargetEndpointAddress("http://www.webxml.com.cn/WebServices/RandomFontsWebService.asmx?wsdl"); //设置目标接口的地址
        call.setSOAPActionURI("http://WebXml.com.cn/getCharFonts");
        call.setEncodingStyle("utf-8");//设置传入服务端的字符集格式如utf-8等
        call.setOperationName(new QName("http://WebXml.com.cn/", "getCharFonts"));//设置命名空间和方法名
        call.setUseSOAPAction(true);
        call.addParameter(new QName("http://WebXml.com.cn/", "byFontsLength"), XMLType.XSD_INTEGER, ParameterMode.IN);
        call.setReturnClass(java.lang.String[].class); //返回字符串数组类型
        String[] result = (String[]) call.invoke(new Object[]{"1"});
        System.out.println(JSON.toJSONString(result));
    }

    public static void test002() throws Exception {
        Service service = new Service();
        Call call = (Call) service.createCall();
        call.setTargetEndpointAddress("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl"); //设置目标接口的地址
        call.setSOAPActionURI("http://WebXml.com.cn/getMobileCodeInfo");
        call.setEncodingStyle("utf-8");//设置传入服务端的字符集格式如utf-8等
        call.setOperationName(new QName("http://WebXml.com.cn/", "getMobileCodeInfo"));//设置命名空间和方法名
        call.setUseSOAPAction(true);
        call.addParameter(new QName("http://WebXml.com.cn/", "mobileCode"), XMLType.XSD_STRING, ParameterMode.IN);
        call.addParameter(new QName("http://WebXml.com.cn/", "userID"), XMLType.XSD_STRING, ParameterMode.IN);
        call.setReturnType(XMLType.XSD_STRING);
        String result = (String) call.invoke(new Object[]{"13780200379", ""});
        System.out.println(result);
    }
}
